package cn.tsmchina.one.controller.m;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tsmchina.one.domain.config.Config;
import cn.tsmchina.one.domain.recommendation.Recommend;
import cn.tsmchina.one.domain.rproduct.Rproduct;
import cn.tsmchina.one.domain.user.User;
import cn.tsmchina.one.pojo.RecommendPojo;
import cn.tsmchina.one.service.config.ConfigService;
import cn.tsmchina.one.service.recommendation.RecommentService;
import cn.tsmchina.one.service.user.CustomerService;
import cn.tsmchina.one.util.C;
import cn.tsmchina.one.util.ConfigUtil;
import cn.tsmchina.one.util.JsonUtil;
import cn.tsmchina.one.util.LoginUtil;
import cn.tsmchina.one.util.PayParams;

@Controller
public class RecommendmController
{
	@Autowired
	private RecommentService recommendService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private ConfigService configService;	
	
	/**
	 * 推荐列表信息查询
	 * @param model
	 * @return
	 */
	@RequestMapping("/recommend")
	public String recommend(Model model,HttpServletRequest request)
	{
		System.out.println("进入推荐列表");
		long cid = LoginUtil.getCustomerId(request, customerService);
		if(cid==-1) {
			return "m/login";
		}
		int page = 1;
		String curPage = request.getParameter("page");
		if(!StringUtils.isEmpty(curPage)){
			page = Integer.valueOf(curPage);
		}
		int total = recommendService.getTotal();
		if(total<=C.Paging.RECOMMEND_PAGE_SIZE) {
			request.setAttribute("enablePage", false);
		} else {
			request.setAttribute("enablePage", true);
			if(page==1) {
				request.setAttribute("enableFirst", false);
			} else {
				request.setAttribute("enableFirst", true);
				request.setAttribute("prev", page-1);
			}
			
			int pageTotal = total % C.Paging.RECOMMEND_PAGE_SIZE == 0 ? total / C.Paging.RECOMMEND_PAGE_SIZE : total / C.Paging.RECOMMEND_PAGE_SIZE + 1;
			if(page>=pageTotal) {
				request.setAttribute("enableLast", false);
			} else {
				request.setAttribute("enableLast", true);
				request.setAttribute("next", page+1);
			}
		}
		
		List<RecommendPojo> recommends = recommendService.getRecs(page, cid);
		request.setAttribute("recommends", recommends);
		
		
		
		return "m/recommend";
	}
	
	/**
	 * 推荐商品详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/recommenddetail/{id}")
	public String recommenddetail(@PathVariable("id") long id,Model model,HttpServletRequest request)
	{
		Recommend recommend = recommendService.getRec((int) id);
		
		if(recommend==null)
			return "m/404";
		Map<String, Object> data = new HashMap<>();
		data.put("id", recommend.getId());
		data.put("recName", recommend.getRecName());
		//data.put("price",recommend.getSummary());
		//为何要强制转换？data.put("summary",Integer.parseInt(recommend.getSummary()));
		data.put("summary",recommend.getSummary());
		data.put("number", 1);
		data.put("images", recommend.getImages());
		//data.put("", value)
		model.addAttribute("p", recommend);
		model.addAttribute("product", JsonUtil.parseJson(data));
		model.addAttribute("title", recommend.getRecName());		
		
		return "m/recommenddetail";
	}	
	
	/**
	 * 推荐商品购物车（由于数据库表不同，需要区分）
	 * @param model
	 * @return
	 */
	@RequestMapping("/recCart")	
	public String recCart(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if(user!=null) {
			request.setAttribute("credits", user.getCredits());
		} else {
			request.setAttribute("credits", "<a href='user/login?cid='>请登录后查看积分</a>");
		}
		return "m/cart_rec";
	}

	@RequestMapping("/recOrderInfo")
	public String orderInfo(String shopInfo, double totalPrice, int pcount, HttpServletRequest request) throws UnsupportedEncodingException
	{
		User user = (User) request.getSession().getAttribute("user");
		if(user!=null) {
			request.setAttribute("host", "http://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath());
			request.setAttribute("user", user);
			request.setAttribute("credits", user.getCredits());
			request.setAttribute("totalPrice", totalPrice);
			request.setAttribute("pcount", pcount);
			shopInfo = java.net.URLDecoder.decode(shopInfo, "utf-8");
			request.setAttribute("shopInfo", shopInfo);

		}
		
		//微信支付添加
		Config wcpay = configService.getConfig(1, C.ConfigKeys.WC_PAY);
		if(null!=wcpay){
			request.setAttribute("wcpay", "yes");
			request.setAttribute("wcpayNotifyUrl", ConfigUtil.get("wcpayNotifyUrl"));
			
			PayParams wxpay = JsonUtil.parseObject(PayParams.class, wcpay.getConfigValue());
			request.setAttribute("wxpay", wxpay);
		}
		
		return "m/orderInfo_rec";
	}	
	
}